//
//  SEMyRegistrationListViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/15.
//

import UIKit

class SEMyRegistrationListViewController: SEBaseViewController {

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableFooterView = UIView()
            tableView.rowHeight = UITableView.automaticDimension
            tableView.se_header = SERefreshHeader { [weak self] in self?.loadNewData() }
            tableView.se_footer = SERefreshFooter { [weak self] in self?.loadMoreData() }
            tableView.se_empty = SEEmptyView { [weak self] in self?.requestMyRegistrationList() }
            tableView.register(UINib(nibName: SEMyRegistrationListCell.className, bundle: nil), forCellReuseIdentifier: SEMyRegistrationListCell.reuseId)
        }
    }

    var viewModel = SEMyRegistrationViewModel(withLoading: false)

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "我的挂号"
        requestMyRegistrationList()
        // Do any additional setup after loading the view.
    }

    override func setupUI() {
        super.setupUI()

        viewModel.updateData = { [weak self] status in
            self?.tableView.se_empty?.allowShow = true
            self?.endRefreshing()
            switch status {
            case .success:
                self?.tableView.reloadData()
            case .auth(let msg):
                self?.presentLoginViewController(selector: #selector(self?.requestMyRegistrationList)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }

        viewModel.cancelHandler = {status in
            switch status {
            case .success:
                SEHUD.show(message: "取消成功")
                self.requestMyRegistrationList()
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.requestCancelRegistration)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    private func pushRegistrationDetailViewController() {
        let vc = SERegistrationDetailController(with: viewModel.selectedModel.id, type: .detail(status: viewModel.selectedModel.flag))
        vc.canceledHandler = {
            self.requestMyRegistrationList()
        }
        navigationController?.pushViewController(vc, animated: true)
    }

}

// MARK: - UITableViewDataSource, UITableViewDelegate

extension SEMyRegistrationListViewController: UITableViewDataSource, UITableViewDelegate {

    // MARK: - UITableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.numberOfSections
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SEMyRegistrationListCell.reuseId, for: indexPath) as! SEMyRegistrationListCell
        cell.delegate = self
        cell.updateCell(model: viewModel.registrationModel(indexPath: indexPath), indexPath: indexPath)
        return cell
    }

    // MARK: - UITableViewDelegate

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        return view
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = viewModel.registrationModel(indexPath: indexPath)
        viewModel.selectedModel = model
        pushRegistrationDetailViewController()
    }

}

// MARK: - SEMyRegistrationListCellDelegate

extension SEMyRegistrationListViewController: SEMyRegistrationListCellDelegate {
    func myRegistrationListCellCancelAction(indexPath: IndexPath) {
        let model = viewModel.registrationModel(indexPath: indexPath)
        viewModel.selectedModel = model
        requestCancelRegistration()
    }
}

// MARK: - APIRequest
extension SEMyRegistrationListViewController {
    @objc private func requestMyRegistrationList() {
        tableView.se_header?.beginRefreshing()
    }

    private func endRefreshing() {
        tableView.se_footer_hidden = false
        tableView.endHeaderRefreshing()
        tableView.endFooterRefreshing(hasMore: viewModel.hasMore)
    }

    private func loadNewData() {
        viewModel.page = 1
        viewModel.requestMyRegistrationList()
    }

    private func loadMoreData() {
        viewModel.page += 1
        viewModel.requestMyRegistrationList()
    }

    @objc func requestCancelRegistration() {
        viewModel.requestCancelRegistration()
    }
}

